/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * CodeForm.java * * Created on 28.09.2010, 15:13:39 */ package vizzy.forms.panels; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Rectangle; import java.util.List; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.text.BadLocationException; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; import javax.swing.text.StyledDocument; import org.apache.log4j.Logger; import vizzy.model.Conf; import vizzy.util.StringUtils; /** * * @author sergeil */ public class CodeForm extends JPanel { private static final Logger log = Logger.getLogger(CodeForm.class); private int highlightedLinePos; /** Creates new form CodeForm */ public CodeForm() { setBackground(null); initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jCodeTextPane = new vizzy.comp.NoWordWrapTextPane(); setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3)); jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane1.setViewportView(jCodeTextPane); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE) ); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextPane jCodeTextPane; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables public void updateSize(int height) { Dimension dim = jCodeTextPane.getPreferredScrollableViewportSize(); jCodeTextPane.setPreferredSize(dim); int w = Math.max(Math.min(400, dim.width) + 50, 100); dim = new Dimension(w + jScrollPane1.getVerticalScrollBar().getPreferredSize().width, height); setPreferredSize(dim); setBounds(0, 0, dim.width, dim.height); } public void initStyles(Font font, Color foreground, Color background) { StyledDocument doc = jCodeTextPane.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext(). getStyle(StyleContext.DEFAULT_STYLE); Style boldStyleName = doc.addStyle("bold", def); StyleConstants.setBold(boldStyleName, true); StyleConstants.setForeground(boldStyleName, Color.RED); jCodeTextPane.setFont(font); jCodeTextPane.setForeground(foreground); jCodeTextPane.setBackground(background); } public void setText(List<String> lines, int boldLineNum) { try { StyledDocument doc = jCodeTextPane.getStyledDocument(); highlightedLinePos = -1; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i) + Conf.newLine; if (i == boldLineNum) { highlightedLinePos = doc.getLength(); doc.insertString(highlightedLinePos, line, doc.getStyle("bold")); highlightedLinePos += (line.length() - StringUtils.trimStart(line).length()); } else { int len = doc.getLength(); doc.insertString(len, line, doc.getStyle("regular")); } } } catch (BadLocationException ex) { // log.warn("setText()", ex); } } public void initUI() { jCodeTextPane.requestFocus(); } public void scrollText() { if (highlightedLinePos != -1) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { Rectangle rect = jCodeTextPane.modelToView(highlightedLinePos); jScrollPane1.getHorizontalScrollBar().setValue((int) (rect.getX() / 3 * 2)); jScrollPane1.getVerticalScrollBar().setValue((int) (rect.getY() - getHeight() / 2)); } catch (BadLocationException ex) { // log.warn("cannot scroll text, scrollText()", ex); } } }); } } public void setFocus() { jCodeTextPane.requestFocus(); } public void setText(String content) { jCodeTextPane.setText(content); } }